<?php

/* @project : Template
 * @auther  : 青云
 * @date    : 2018/1/10
 * @desc    : 菜单控制器
 */

namespace app\admin\controller;

use think\Request;
use app\common\model\SystemGroup as SystemGroupModel;

class SystemGroup extends Base
{

    function add()
    {
//        dump((new \app\common\model\SystemGroupData())->getKeyValListByCode('member_group'));
//        die;

        $module_arr = config('module_arr');
        $this->assign('module_arr', $module_arr);

        $module = input('module');
        $this->assign('module', $module);

        return $this->fetch();
    }

    function edit(Request $request)
    {
        $vo = $this->_edit($request);

        $field_list = json_decode($vo['fields'], true);

        $this->assign('field_list', $field_list);

        return $this->fetch();
    }

    function save(Request $request)
    {
        $field_name  = $request->field_name;
        $field_code  = $request->field_code;
        $field_type  = $request->field_type;
        $field_param = $request->field_param;

        foreach ($field_name as $key => $name) {
            $fields[$key] = ['field_name' => $field_name[$key], 'field_code' => $field_code[$key], 'field_type' => $field_type[$key], 'field_param' => $field_param[$key]];
        }

        $request->fields = json_encode($fields, JSON_UNESCAPED_UNICODE);

        return $this->_save($request);
    }

    function del(Request $request)
    {
        $result = (new SystemGroupModel())->del($request->id);
        if ($result !== null) {
            return $this->api_success('删除成功');
        } else {
            return $this->api_success('删除失败');
        }
    }

    function sort_up(Request $request)
    {
        return $this->_sort_up($request);
    }

    function sort_down(Request $request)
    {
        return $this->_sort_down($request);
    }
}
